<template>
    <a-drawer
        :title="formData.jgh ? '编辑教工信息' : '增加教工信息'"
        :width="600"
        :visible="visible"
        :destroy-on-close="true"
        :footer-style="{ textAlign: 'right' }"
        @close="onClose"
    >
        <a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
            <a-form-item label="姓名：" name="xm">
                <a-input v-model:value="formData.xm" placeholder="请输入姓名" allow-clear />
            </a-form-item>
            <a-form-item label="曾用名：" name="cym">
                <a-input v-model:value="formData.cym" placeholder="请输入曾用名" allow-clear />
            </a-form-item>
            <a-form-item label="性别：" name="xb">
                <a-input v-model:value="formData.xb" placeholder="请输入性别" allow-clear />
            </a-form-item>
            <a-form-item label="性别代码：" name="xbdm">
                <a-input v-model:value="formData.xbdm" placeholder="请输入性别代码" allow-clear />
            </a-form-item>
            <a-form-item label="出生日期：" name="csrq">
                <a-input v-model:value="formData.csrq" placeholder="请输入出生日期" allow-clear />
            </a-form-item>
            <a-form-item label="民族：" name="mz">
                <a-input v-model:value="formData.mz" placeholder="请输入民族" allow-clear />
            </a-form-item>
            <a-form-item label="民族代码：" name="mzdm">
                <a-input v-model:value="formData.mzdm" placeholder="请输入民族代码" allow-clear />
            </a-form-item>
            <a-form-item label="身份类型：" name="sfzjlxm">
                <a-input v-model:value="formData.sfzjlxm" placeholder="请输入身份类型" allow-clear />
            </a-form-item>
            <a-form-item label="证件号码：" name="sfzjh">
                <a-input v-model:value="formData.sfzjh" placeholder="请输入证件号码" allow-clear />
            </a-form-item>
            <a-form-item label="教工类型名称：" name="jzglbmc">
                <a-input v-model:value="formData.jzglbmc" placeholder="请输入教工类型名称" allow-clear />
            </a-form-item>
            <a-form-item label="教工类别代码：" name="jzglbdm">
                <a-input v-model:value="formData.jzglbdm" placeholder="请输入教工类别代码" allow-clear />
            </a-form-item>
            <a-form-item label="教师职称：" name="jszc">
                <a-input v-model:value="formData.jszc" placeholder="请输入教师职称" allow-clear />
            </a-form-item>
            <a-form-item label="教师职称代码：" name="jszcdm">
                <a-input v-model:value="formData.jszcdm" placeholder="请输入教师职称代码" allow-clear />
            </a-form-item>
            <a-form-item label="当前状态：" name="dqzt">
                <a-input v-model:value="formData.dqzt" placeholder="请输入当前状态" allow-clear />
            </a-form-item>
            <a-form-item label="状态代码：" name="dqztdm">
                <a-input v-model:value="formData.dqztdm" placeholder="请输入状态代码" allow-clear />
            </a-form-item>
            <a-form-item label="所属单位名称：" name="dwmc">
                <a-input v-model:value="formData.dwmc" placeholder="请输入所属单位名称" allow-clear />
            </a-form-item>
            <a-form-item label="单位代码：" name="dwh">
                <a-input v-model:value="formData.dwh" placeholder="请输入单位代码" allow-clear />
            </a-form-item>
        </a-form>
        <template #footer>
            <a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
            <a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
        </template>
    </a-drawer>
</template>

<script setup name="jwJzgxxForm">
    import { cloneDeep } from 'lodash-es'
    import { required } from '@/utils/formRules'
    import jwJzgxxApi from '@/api/biz/jwJzgxxApi'
    // 抽屉状态
    const visible = ref(false)
    const emit = defineEmits({ successful: null })
    const formRef = ref()
    // 表单数据
    const formData = ref({})
    const submitLoading = ref(false)

    // 打开抽屉
    const onOpen = (record) => {
        visible.value = true
        if (record) {
            let recordData = cloneDeep(record)
            formData.value = Object.assign({}, recordData)
        }
    }
    // 关闭抽屉
    const onClose = () => {
        formRef.value.resetFields()
        formData.value = {}
        visible.value = false
    }
    // 默认要校验的
    const formRules = {
    }
    // 验证并提交数据
    const onSubmit = () => {
        formRef.value
            .validate()
            .then(() => {
                submitLoading.value = true
                const formDataParam = cloneDeep(formData.value)
                jwJzgxxApi
                    .jwJzgxxSubmitForm(formDataParam, formDataParam.jgh)
                    .then(() => {
                        onClose()
                        emit('successful')
                    })
                    .finally(() => {
                        submitLoading.value = false
                    })
            })
    }
    // 抛出函数
    defineExpose({
        onOpen
    })
</script>
